<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: PortalUserOpenIdRecord.php 430 2011-01-05 11:57:26Z steen.rabol $
 */

Prado::using('Application.Common.Data.PortalActiveRecord');

class PortalUserOpenIdRecord extends PortalActiveRecord
{
	const TABLE	= 'tbluseropenids';

	public $id;
	public $openid_url	= "";
	public $userid;

	public static function finder($className=__CLASS__)
	{
		return parent::finder($className);
	}

	public static function GetUserId($openid_url)
	{
		//select user_id from user_openids where openid_url = openid_url
		$db = Prado::getApplication()->DbConnection;
		$sql = "select userid from tbluseropenids where openid_url = '$openid_url'";
		try
		{
			return $db->createCommand($sql)->queryScalar();
		}
		catch(Exception $e)
		{
			return 0;
		}
	}

	public static function GetOpenIDsByUser($userid)
	{
		//select openid_url from user_openids where user_id = user_id
		return PortalUserOpenIdRecord::finder()->findAll("userid = ?",$userid);
	}

	public static function AttachOpenID($openid_url, $userid)
	{
		//insert into user_openids values (openid_url, user_id)
		$ir = new PortalUserOpenIdRecord();
		$ir->userid	= $userid;
		$ir->openid_url	= $openid_url;
		$ir->save();
	}

	public static function DetachOpenID($openid_url, $userid)
	{
		//delete from user_openids where openid_url = openid_url and user_id = user_id
		$db = Prado::getApplication()->DbConnection;
		$sql = "delete from tbluseropenids where openid_url = $openid_url and userid = $userid";
		try
		{
			$db->createCommand($sql)->execute();
		}
		catch(Exception $e)
		{
			return 0;
		}
	}

	public static function DetachOpenIDsByUser($userid)
	{
		//delete from user_openids where user_id = user_id
		$db = Prado::getApplication()->DbConnection;
		$sql = "delete from tbluseropenids where userid = $userid";
		try
		{
			$db->createCommand($sql)->execute();
		}
		catch(Exception $e)
		{
			return 0;
		}
	}

	public static function GetUserName($id)
	{
		//select user_id from user_openids where openid_url = openid_url
		$db = Prado::getApplication()->DbConnection;
		$sql = "select name from tblusers where id = $id";
		try
		{
			return $db->createCommand($sql)->queryScalar();
		}
		catch(Exception $e)
		{
			return 0;
		}
	}

}
?>